﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Baosight.Ccs.Mail.Properties;

namespace Baosight.Ccs.Mail
{
    /// <summary>
    /// 黑名单 spam handler
    /// <remarks>
    /// 如果邮件来源于spam list中的地址，则将邮件标示为已处理
    /// </remarks>
    /// </summary>
    public class BlackListHandler : IMailHandler
    {
        private List<string> blackList = new List<string>();

        public BlackListHandler()
        {
            foreach (string spam in Settings.Default.SpamList)
            {
                blackList.Add(spam.ToLower());
            }
        }

        public void AddToBlackList(string mailAddress)
        {
            if (!blackList.Contains(mailAddress.ToLower()))
            {
                blackList.Add(mailAddress.ToLower());
            }
        }

        public void RemoveFromBlackList(string mailAddress)
        {
            blackList.RemoveAll(item => item == mailAddress.ToLower());
        }

        #region IMailHandler Members

        public bool Process(Mail mail)
        {
            if( blackList.Contains(mail.FromAddress.ToLower()))
            {
                MailManager.Instance.ChangeMailStatus(mail,MailStatus.Handled);
                return true;
            }
            return false;
        }

        #endregion
    }
}
